package com.block.merchant.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.service.LogFactory;
import com.block.common.vo.ResponseVo;
import com.block.db.entity.MerchantBaseInfo;
import com.block.db.entity.MerchantDetailInfo;
import com.block.db.mapper.MerchantBaseInfoMapper;
import com.block.db.mapper.MerchantServeCityInfoMapper;
import com.block.merchant.model.vo.MerchantInfoVO;
import com.block.merchant.service.IMerchantBaseInfoService;
import com.block.merchant.service.IMerchantDetailInfoService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商户基本信息 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-08-12
 */
@Service
public class MerchantBaseInfoServiceImpl extends ServiceImpl<MerchantBaseInfoMapper, MerchantBaseInfo> implements IMerchantBaseInfoService {

    private static final Logger log = LogFactory.getMerchantLog();
    @Resource
    MerchantServeCityInfoMapper merchantServeCityInfoMapper;
    @Autowired
    IMerchantDetailInfoService merchantDetailInfoService;

    @Override
    public ResponseVo getPageDataList(String cityCode) {

        // 1. 根据地址信息 查询满足条件的 商户信息
        List<Integer> infoIdList = merchantServeCityInfoMapper.getInfoIdListByCityCode(cityCode);

        List<MerchantInfoVO> dataList = new ArrayList<>();
        if (CollUtil.isNotEmpty(infoIdList)){
            for (Integer infoId : infoIdList) {
                MerchantDetailInfo detailInfo = merchantDetailInfoService.getEntityByInfoId(infoId);

                MerchantInfoVO merchantInfoVO = new MerchantInfoVO();
                merchantInfoVO.setInfoId(infoId);
                merchantInfoVO.setProductLogo(detailInfo.getProductLogo());
                merchantInfoVO.setProductName(detailInfo.getProductName());
                merchantInfoVO.setCompanyName(detailInfo.getCompanyName());
                merchantInfoVO.setLabelInfo(detailInfo.getLabelInfo());
                merchantInfoVO.setAddressInfo(detailInfo.getAddressInfo());

                dataList.add(merchantInfoVO);
            }
        }

        JSONObject returnData = new JSONObject();
        returnData.put("dataList", dataList);

        return new ResponseVo(AppRspCodeConstant.SUCCESS, returnData);
    }
}
